package com.yin.waimai.controller;

import com.yin.waimai.dto.DeliveryPersonDTO;
import com.yin.waimai.dto.OrderDTO;
import com.yin.waimai.dto.UserDTO;
import com.yin.waimai.service.DeliveryPersonService;
import com.yin.waimai.service.OrderService;
import com.yin.waimai.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping("/api/v1/admin")
// @PreAuthorize("hasRole('ADMIN')") // 临时注释掉这一行
public class AdminController {

    private static final Logger log = LoggerFactory.getLogger(AdminController.class);

    private final UserService userService;
    private final DeliveryPersonService deliveryPersonService;
    private final OrderService orderService;

    @Autowired
    public AdminController(UserService userService, DeliveryPersonService deliveryPersonService, OrderService orderService) {
        this.userService = userService;
        this.deliveryPersonService = deliveryPersonService;
        this.orderService = orderService;
    }

    /**
     * 检查管理员权限 - 临时版本，不做实际检查
     */
    @GetMapping("/check-auth")
    public ResponseEntity<?> checkAuth() {
        log.info("临时跳过管理员权限检查");
        Map<String, Object> response = new HashMap<>();
        response.put("username", "admin");
        response.put("role", "ADMIN");
        return ResponseEntity.ok(response);
    }

    /**
     * 获取系统统计数据
     */
    @GetMapping("/statistics")
    public ResponseEntity<?> getStatistics() {
        log.info("Getting system statistics");
        
        Map<String, Object> statistics = new HashMap<>();
        statistics.put("totalUsers", userService.countUsers());
        statistics.put("totalDeliveryPersons", deliveryPersonService.countDeliveryPersons());
        statistics.put("totalOrders", orderService.countOrders());
        statistics.put("completedOrders", orderService.countCompletedOrders());
        
        return ResponseEntity.ok(statistics);
    }

    /**
     * 获取最近订单
     */
    @GetMapping("/orders/recent")
    public ResponseEntity<List<OrderDTO>> getRecentOrders() {
        log.info("Getting recent orders");
        List<OrderDTO> recentOrders = orderService.getRecentOrders(10); // 获取最近10个订单
        return ResponseEntity.ok(recentOrders);
    }

    /**
     * 获取活跃配送员
     */
    @GetMapping("/delivery-persons/active")
    public ResponseEntity<List<DeliveryPersonDTO>> getActiveDeliveryPersons() {
        log.info("Getting active delivery persons");
        List<DeliveryPersonDTO> activeDeliveryPersons = deliveryPersonService.getActiveDeliveryPersons();
        return ResponseEntity.ok(activeDeliveryPersons);
    }

    /**
     * 获取所有用户
     */
    @GetMapping("/users")
    public ResponseEntity<List<UserDTO>> getAllUsers() {
        log.info("Getting all users");
        List<UserDTO> users = userService.findAllUsers();
        return ResponseEntity.ok(users);
    }

    /**
     * 获取所有订单
     */
    @GetMapping("/orders")
    public ResponseEntity<List<OrderDTO>> getAllOrders() {
        log.info("Getting all orders");
        List<OrderDTO> orders = orderService.findAllOrders();
        return ResponseEntity.ok(orders);
    }

    /**
     * 获取所有配送员
     */
    @GetMapping("/delivery-persons")
    public ResponseEntity<List<DeliveryPersonDTO>> getAllDeliveryPersons() {
        log.info("Getting all delivery persons");
        List<DeliveryPersonDTO> deliveryPersons = deliveryPersonService.findAll();
        return ResponseEntity.ok(deliveryPersons);
    }
}